In object-oriented programming, a method is a subroutine (or procedure or function) associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time. Methods have the special property that at runtime, they have access to data stored in an instance of the class (or class instance or class object or object) they are associated with and are thereby able to control the state of the instance. [1] The association between class and method is called binding. A method associated with a class is said to be bound to the class. Methods can be bound to a class at compile time (static binding) or to an object at runtime (dynamic binding).[2]
Contents |
An abstract method is one with only a signature and no implementation body. It is often used to specify that a subclass must provide an implementation of the method. Abstract methods are used to specify interfaces in some computer languages.[3]
An 'accessor' method is a method that is usually small, simple and provides the sole means for the state of an object to be accessed (retrieved) from other parts of a program.
getBalance()
accessor method to retrieve the current balance (rather than directly accessing the balance data fields), then later revisions of the same code can implement a more complex mechanism for balance retrieval (say, a database fetch), without the dependent code needing to be changed (However, this often claimed advantage is not unique to object oriented programming, and was earlier implemented - when desirable in critical systems - through conventional modular programming with optional run-time, system-wide locking mechanisms, in the imperative/procedural paradigms)An update, modifier, or mutator method, is an accessor method that changes the state of an object. Objects that provide such methods are considered mutable objects.
Class methods are methods that are called on a class (compare this to class instance methods, or object methods). Its meaning may vary depending on the programming language:
this
cannot be used in static methods.this
refers to the class object.classmethod
and staticmethod
decorators, respectively. The former has access to this
, while the latter does not.A conversion operator provides a means for the compiler to implicitly (performed by the compiler automatically when appropriate) provide an object of a type other than the type of the class object.
Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters.
Overloaded methods are those with the same name but different formal parameters or return value type, if the language supports overloading on return type.[4]
Overridden methods are those that are redefined in a subclass and hide methods of a superclass.[5]
Special methods are very language-specific and a language may support none, some, or all of the special methods defined here. A language's compiler may automatically generate default special methods or a programmer may be allowed to optionally define special methods. Most special methods cannot be directly called, but rather the compiler generates code to call them at appropriate times. The syntax for definition and calling (i.e., when a special method can be called) of special methods varies amongst programming languages.
A constructor is a class method that is called automatically at the beginning of an object's lifetime to initialize the object, a process called construction (or instantiation). Initialization may include acquisition of resources. A language may provide a means to control whether a constructor can be called implicitly (by the compiler) or only explicitly (by the programmer). Constructors may have parameters but usually do not return values in most languages.
A destructor is a class method that is called automatically at the end of an object's lifetime, a process called destruction. Destructors in most languages do not allow destructor method arguments nor return values. (In some languages (ref required), a destructor can return a value which can then be used to obtain a public representation (transfer encoding) of an instance of a class and simultaneously destroy the copy of the instance stored in current thread's memory.) Destructors can be implemented such as to perform clean up chores and other tasks at object destruction.
Copy-assignment operators define actions to be performed by the compiler when a class object is assigned to a class object of the same type.
Static methods neither require an instance of the class nor can they implicitly access the data (or this
, self
, Me
, etc.) of such an instance. A static method is distinguished in some programming languages with the static
keyword placed somewhere in the method's signature. Static methods are called "static" because they are resolved statically (i.e. at compile time) based on the class they are called on; and not dynamically, as in the case with instance methods which are resolved polymorphically based on the runtime type of the object. Therefore, static methods cannot be overridden.
Virtual methods are the means by which a class object can achieve polymorphic behavior. Non-virtual methods, or regular methods, are those which do not participate in polymorphism. [6]